package com.elric.mall.controller;

import com.elric.mall.common.api.BaseController;
import com.elric.mall.common.api.CommonPage;
import com.elric.mall.common.api.CommonResult;
import com.elric.mall.enums.ImportType;
import com.elric.mall.model.ZgBaseZzjg;
import com.elric.mall.service.ZgBaseZzjgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 实现功能：战规-组织机构控制层
 *
 * @author 作者[Yuxuan.he]
 * @version 1.0.00  2021-09-16 10:44
 */
@Api(tags = "ZgBaseZzjgController", description = "战规-组织机构接口")
@RequestMapping("/ZgBaseZzjg")
@RestController
public class ZgBaseZzjgController extends BaseController {

    @Autowired
    ZgBaseZzjgService zgBaseZzjgService;

    @ApiOperation("添加数据")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody ZgBaseZzjg zgBaseZzjg) {
        int count = zgBaseZzjgService.create(zgBaseZzjg);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("修改数据")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@RequestBody ZgBaseZzjg zgBaseZzjg) {
        int count = zgBaseZzjgService.update(zgBaseZzjg);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("根据ID获取详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<ZgBaseZzjg> getItem(@PathVariable String id) {
        ZgBaseZzjg ZgBaseZzjg = zgBaseZzjgService.findById(id);
        return CommonResult.success(ZgBaseZzjg);
    }

    @ApiOperation("根据ID删除数据")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@PathVariable String id) {
        int count = zgBaseZzjgService.delete(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("分页查询数据")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<ZgBaseZzjg>> list(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                            @RequestBody ZgBaseZzjg zgBaseZzjg) {
        List<ZgBaseZzjg> menuList = zgBaseZzjgService.list(pageSize, pageNum, zgBaseZzjg);
        return CommonResult.success(CommonPage.restPage(menuList));
    }

    @ApiOperation("批量导入")
    @RequestMapping(value = "/insertBatch", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult insertBatch(@RequestParam(value = "importType", defaultValue = "1") ImportType type,
                                    @RequestBody List<ZgBaseZzjg> list) {
        return CommonResult.success(zgBaseZzjgService.insertBatch(list, type));
    }

    @ApiOperation("查询组织机构树形")
    @RequestMapping(value = "/findZzjgTree", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult findProvincesTree() {
        return CommonResult.success(zgBaseZzjgService.findeZzjgTree());
    }
}
